package com.learn.code.sign;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author : 翌擎科技
 * @date : 2022/8/8 15:13
 */
public class SignUtils {

    public static String signature1(InvoiceRequest invoiceRequest, String omsAppSecret) {
        String data = invoiceRequest.getData();
        String appKey = invoiceRequest.getAppKey();
        String timestamp = invoiceRequest.getTimestamp();
        String method = invoiceRequest.getMethod();

        StringBuilder sbr = new StringBuilder();
//        if(StringUtils.isNotEmpty(appSecret)){
//            sbr.append(appSecret);
//        }
        sbr.append(omsAppSecret);
        sbr.append(appKey);
        sbr.append(method);
        sbr.append(timestamp);
        sbr.append(data);
        sbr.append(omsAppSecret);
//        if(StringUtils.isNotEmpty(appSecret)){
//            sbr.append(appSecret);
//        }
        System.out.println(sbr.toString());
        String sign =getMD5(sbr.toString());
        return sign;
    }

    public static String signature2(InvoiceRequest invoiceRequest, String omsAppSecret) {
        String data = invoiceRequest.getData();
        String appKey = invoiceRequest.getAppKey();
        String timestamp = invoiceRequest.getTimestamp();
        StringBuilder sbr = new StringBuilder();
        sbr.append(omsAppSecret);
        sbr.append(appKey);
        sbr.append(timestamp);
        sbr.append(data);
        sbr.append(omsAppSecret);
        System.out.println(sbr.toString());
        String sign = getMD5(sbr.toString());
        return sign;
    }

    public static String getMD5(String code) {
        String md = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] bytes = code.getBytes();
            byte[] results = messageDigest.digest(bytes);
            StringBuilder stringBuilder = new StringBuilder();

            for (byte result : results) {
                // 将byte数组转化为16进制字符存入stringbuilder中
                stringBuilder.append(String.format("%02x", result));
            }
            md = stringBuilder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        return md;
    }

    public static void main(String[] args) {
        InvoiceRequest invoiceRequest=new InvoiceRequest();
        invoiceRequest.setAppKey("guanwang-o");
        invoiceRequest.setAppKey("cb4dfdf40dddf18a53f058337f512d29");
        String s = System.currentTimeMillis() + "";
        System.out.println(s);
        invoiceRequest.setTimestamp("1659943285231");
//        invoiceRequest.setData("{\"orderType\":\"1021\",\"orderNumber\":\"1556537343732568064\",\"serviceShopNo\":\"911010\",\"color\":\"太阳金乌黑\",\"newShopNo\":\"911010\",\"configuration\":\"中配\",\"contractLockState\":\"0\",\"engineType\":\"BEV_PE396156A\",\"remark\":\"购车方式：个人购买；姓名：杨洋；证件类型：身份证；证件号码：110101199003075234；意向金融方案：；首付比例：；首付金额：；贷款金额：；还款期数：；月供金额：；尾款金额：；保险预估总费用：；电池方案：；电池方案价格：；\",\"randomCode\":\"-1024\",\"enrollerType\":\"1\",\"customerPhone\":\"17631569371\",\"carType\":\"CC7000BJ02EBEV\",\"orderAmount\":\"154000.0000\",\"verifContractLockState\":\"0\",\"customerSourceThrid\":\"订单\",\"enrollerTypeName\":\"个人\",\"customerSourceFirst\":\"厂家自有平台\",\"brand\":\"欧拉ES11GT\",\"enrollerTel\":\"17631569371\",\"enrollerName\":\"杨洋\",\"enrollerId\":\"110101199003075234\",\"issuingTime\":\"1659942407\",\"customerNumber\":\"-1024\",\"customerName\":\"李荣浩\",\"enrollerCertType\":\"1\",\"newShopName\":\"重庆长城汽车销售服务有限公司北京分公司\",\"serviceShopName\":\"重庆长城汽车销售服务有限公司北京分公司\",\"partten\":\"2022款-黑/红内饰\",\"name\":\"李荣浩\",\"deposit\":\"0.03\",\"operationType\":\"1\",\"shopNumber\":\"911010\",\"contactPhone\":\"17631569371\",\"customerSourceSecond\":\"欧拉ORA\",\"certificateType\":\"4\",\"option\":\"\"}");
        invoiceRequest.setData("{\"carType\":\"CC7000BJ02EBEV\",\"color\":\"太阳金乌黑\",\"configuration\":\"中配\",\"crmOrderNumber\":\"XSDD2022051700018\",\"partten\":\"2022款-黑/红内饰\",\"engineType\":\"BEV_PE396156A\",\"option\":\"记忆辅助/锁车自动折叠/电动调节/除霜/转向灯,声纹脸部识别,前排座椅加热,前排座椅通风+前排座椅按摩,感应式电动后背门,驾驶员:6方向+记忆辅助+迎宾座椅,驾驶员座椅通风,驾驶员座椅按摩,副驾驶座椅通风,副驾驶:4方向电动,迎宾灯光效果-A,6扬声器\"}");
        invoiceRequest.setMethod("POST");
//        String signature1 = signature1(invoiceRequest, "cb4dfdf40dddf18a53f058337f512d29");
        String signature1 = signature2(invoiceRequest, "cb4dfdf40dddf18a53f058337f512d29");
        System.out.println(signature1);
    }
}
